package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.ArticleDTO;
import com.ruoyi.system.domain.WaNotice;
import com.ruoyi.system.domain.vo.ArticleVo;
import com.ruoyi.system.service.WaNoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/publish/notice")
public class WaNoticeController  extends BaseController {
    @Autowired
    private WaNoticeService noticeService;

//  获取通知公告列表
@PreAuthorize("@ss.hasPermi('system:notice:list')")
@GetMapping("/list")
    @ Anonymous
public TableDataInfo list(WaNotice notice)
{
    startPage();
    List<WaNotice> notices = noticeService.selectNoticeList(notice);
    return getDataTable(notices);
}
    //通过栏目id获取文章
@PreAuthorize("@ss.hasPermi('system:notice:article')")
@GetMapping("/article")
public AjaxResult articleList(@RequestParam("columnId") Long columnId)
{
    try {
        // 参数校验
        if (columnId == null || columnId <= 0) {
            return error("栏目ID不能为空且必须为正数");
        }
        // 构造查询条件对象
        WaNotice query = new WaNotice();
        // 查询文章列表
        List<WaNotice> articles = noticeService.selectArticleByColumnById(query);
        return success(articles);
    } catch (Exception e) {
        return error("获取文章列表失败，请稍后重试");
    }
}

//通过栏目的发布时间获取栏目
@PreAuthorize("@ss.hasPermi('system:notice:article')")
@GetMapping("/column")
public AjaxResult columnList(@RequestParam("publishTime") WaNotice publishTime)
{
    List<WaNotice> columns = noticeService.selectNoticeList(publishTime);
    return success(columns);
}

// 添加通知公告
@PreAuthorize("@ss.hasPermi('system:notice:add')")
@Log(title = "通知公告管理", businessType = BusinessType.INSERT)
@PostMapping
public AjaxResult add(@Validated @RequestBody WaNotice notice)
{
    Long articleId = notice.getArticleId();
    if (articleId == null || articleId <= 0) {
        return error("文章ID不能为空且必须为正数");
    }
    if (noticeService.isArticleIdUsedNotice(articleId)) {
        return error("该文章ID已被通知公告使用，请选择其他文章");
    }
    notice.setCreateBy(getUsername());
    return toAjax(noticeService.insertNotice(notice));
}
//删除通知公告
@PreAuthorize("@ss.hasPermi('system:notice:remove')")
@Log(title = "通知公告管理", businessType = BusinessType.DELETE)
@DeleteMapping("/{noticeId}")
public AjaxResult remove(@PathVariable Long noticeId)
{
    return toAjax(noticeService.deleteNoticeById(noticeId));
}

//查询文章
@GetMapping("/articles")
public AjaxResult selectArticle(ArticleDTO dto){
    List<ArticleVo> list = noticeService.selectArticle(dto);
    return success(noticeService.selectArticle(dto));
}
}
